package com.xigua.xcar.setting;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.net.AbstractDataManager;
import com.xigua.xcar.net.feedback.FeedbackData;
import com.xigua.xcar.net.feedback.FeedbkDataManager;

public class FeedbackActivity extends BaseActivity {
	private static final String tag = "FeedbackActivity";
	private EditText mEdtContent = null;
	private EditText mEdtContact = null;
	private EditText mEdtNickName = null;
	private FeedbkDataManager mDM = null;
	private boolean isGettingData = false;
	private ProgressDialog mProgressDialog = null;
	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		if(null != mDM){
			mDM.setHanlder(null);
			mDM.cancelRequest();
			mDM = null;
		}
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setViewEx(R.layout.feedback,R.layout.titlebarex);
		setTopBarTitle("建议反馈");
		setTitleBar(R.drawable.title_button, "返回", R.drawable.title_button, "发送");
		mDM = new FeedbkDataManager(this);
		mDM.setHanlder(new Handler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				hideProgressDlg();
				isGettingData = false;
				switch(msg.what){
				case AbstractDataManager.RESULTCODE_SUCCESS:{
					FeedbackData data = (FeedbackData)msg.obj;
					if(null != data){
						if(null == data.getContent() || "".equals(data.getContent())){
							Toast.makeText(FeedbackActivity.this, "恭喜您，提交成功", 3000).show();
						}else{
							Toast.makeText(FeedbackActivity.this, data.getContent(), 3000).show();
						}
						FeedbackActivity.this.finish();
					}else{
						Toast.makeText(FeedbackActivity.this, "亲，风筝断线了", 3000).show();
					}
					break;
				}
				case AbstractDataManager.RESULTCODE_NETERR:
				case AbstractDataManager.RESULTCODE_OTHERERR:{
					Toast.makeText(FeedbackActivity.this, "亲，风筝断线了", 3000).show();
					break;
				}
				}
			}
			
		});
		mEdtContent = (EditText)findViewById(R.id.edit_feedback_content);
		mEdtContact = (EditText)findViewById(R.id.edit_feedback_contact);
		mEdtNickName = (EditText)findViewById(R.id.edit_feedback_nickname);
	}

	/**
	 * 显示加载进度条
	 * */
	private void showProgressDlg() {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
			// 设置样式
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			// 设置ProgressDialog 提示信息
			mProgressDialog.setMessage("加载中");
			// 设置ProgressDialog 的进度条是否不明确
			mProgressDialog.setIndeterminate(false);
			// 设置ProgressDialog 是否可以按退回按键取消
			mProgressDialog.setCancelable(true);
			// 让ProgressDialog显示
			mProgressDialog.show();
			mProgressDialog.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					Log.i(tag, "onDismiss");
					cancelRequest();
					mProgressDialog = null;
				}
			});
		}
	}
	
	private void hideProgressDlg(){
		if(null != mProgressDialog){
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}
	
	
	private void sendRequest() {
		if(null != mDM){
			if(!isGettingData){
				isGettingData = true;
				mDM.getDataFromServer();
			}
		}
	}
	
	private void cancelRequest() {
		isGettingData = false;
		mDM.cancelRequest();
	}
	
	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			finish();
			break;
		}
		case Constants.RIGHTBUTTONID: {
			String content = null;
			String contact = null;
			String nickName = null;
			Editable txt = null;
			txt = mEdtContent.getEditableText();
			content = txt.toString();
			txt = mEdtContact.getEditableText();
			contact = txt.toString();
			txt = mEdtNickName.getEditableText();
			nickName = txt.toString();
			if("".equals(content)){
				Toast.makeText(this, "请输入反馈内容", 3000).show();
			}else{
				mDM.setContent(content);
				mDM.setContact(contact);
				mDM.setNickName(nickName);
				showProgressDlg();
				sendRequest();
			}
			break;
		}
		}
	}
	
}
